---
title: "Comparison"
author: "Kohei Watanabe"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r, message=FALSE}
knitr::opts_chunk$set(echo = TRUE, dpi = 150, fig.width = 10, fig.height = 5)
require(dplyr)
require(tidyr)
source("functions.R")
dat <- readRDS('data_index.RDS')
event <- yaml::read_yaml("events.yml")
```

```{r}
dat_sum <- dat %>% as_tibble() %>% 
  filter(country %in% c(country_major, country_minor)) %>% 
  group_by(year) %>% 
  summarise(gti = sum(gti, na.rm = TRUE),
            ls = sum(ls, na.rm = TRUE),
            mids = sum(mids, na.rm = TRUE),
            cinc = sum(cinc, na.rm = TRUE),
            un = sum(un, na.rm = TRUE),
            lrm = sum(lrm_prob, na.rm = TRUE),
            gc = sum(gc, na.rm = TRUE))
mat_sum <- as.matrix(dat_sum)
rownames(mat_sum) <- mat_sum[,1]
mat_sum <- mat_sum[,-1]
```

```{r fig.height=4, fig.width=10}
par(mfrow = c(1, 1), mar = c(2, 4, 1, 1), font.main = 1)
plot_comparison(mat_sum[,c("gti", "lrm", "un")], xlim = c(1940, 2020))
```

```{r fig.height=4, fig.width=10}
par(mfrow = c(1, 1), mar = c(2, 4, 1, 1), font.main = 1)
plot_comparison(mat_sum[,c("gti", "mids", "cinc", "ls")], xlim = c(1860, 2020))
```

```{r, results="asis"}
cor_index(dat, country_major) %>% 
  format_cor() %>% 
  knitr::kable(digits = 3)
```

```{r, results="asis"}
cor_index(dat, country_minor) %>% 
  format_cor() %>% 
  knitr::kable(digits = 3)
```

```{r}
symnum(1, corr = FALSE,
       cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
       symbols = c("***","**","*","."," ")) %>% 
  attr("legend")
```
